package cache

import (
	"sync"
)

// ----------------------------------------
//  Redis 客户端管理器
// ----------------------------------------
type RedisClientManager struct {
	rw      *sync.RWMutex
	clients map[string]*RedisClient
}

func CreateRedisClientManager() *RedisClientManager {
	return &RedisClientManager{rw: &sync.RWMutex{}, clients: make(map[string]*RedisClient)}
}

func (manager *RedisClientManager) Get(name string) *RedisClient {
	manager.rw.RLock()
	defer manager.rw.RUnlock()

	if client, exists := manager.clients[name]; exists {
		return client
	} else {
		return nil
	}
}

func (manager *RedisClientManager) Set(name string, client *RedisClient) {
	manager.rw.Lock()
	defer manager.rw.Unlock()

	manager.clients[name] = client
}

func (manager *RedisClientManager) Clear() {
	manager.rw.Lock()
	defer manager.rw.Unlock()

	for k := range manager.clients {
		delete(manager.clients, k)
	}
}